<template>
    <el-container class="home-container">
<!--      头部-->
        <el-header class="el-header">
            <div class="header-div">
               <img src="../assets/logo.png" alt/>
            </div>
            <span class="header-span">个人健康管理系统</span>
            <el-button type="info" @click="logout">退出系统</el-button>
        </el-header>
<!--        主体-->
        <el-container>
<!--            侧边栏-->
            <el-aside width="200px"  class="el-aside">
                <el-menu background-color="#545c64"
                        text-color="#fff"
                        active-text-color="#ffd04b"
                         class="el-menu"
                unique-opened :router="true" :default-active="activePath">
<!--                    一级菜单-->
                    <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="iconsObject[item.id]"></i>
                            <span>{{item.title}}</span>
                        </template>

<!--                        二级菜单-->

                            <el-menu-item :index="it.path" v-for="it in item.sList" :key="it.id" @click="saveNavState(it.path)">
                                <template slot="title">
                                    <i :class="iconsObject[it.id]"></i>
                                    <span>{{it.title}}</span>
                                </template>
                            </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
<!--            主体内容-->
            <el-main class="el-main">
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<<script>
    export default {
        data() {
            return{
                //菜单列表
                menuList:[],
                iconsObject:{
                    '100':'el-icon-s-check',
                    '200':'el-icon-view',
                    '101':'el-icon-user',
                    '102':'el-icon-s-operation',
                    '201':'el-icon-bicycle',
                    '202':'el-icon-sunny',
                    '203':'el-icon-odometer',
                },
                activePath: '/welcome',//默认路径
            }
        },
        //onload事件
        created() {
            //查询菜单列表
            this.getMenuList();
            this.activePath=window.sessionStorage.getItem('activePath')
        },
        methods: {
            //安全退出
            logout() {
                window.sessionStorage.clear();//清除session
                this.$router.push({path: '../login'});
            },
            async getMenuList() {
                //获取导航菜单
                const {data: res} = await this.$http.get("menus")
                console.log(res);
                if(res.flag!=200)
                    return this.$message.error("获取列表失败！！！");//访问失败的错误信息
                this.menuList=res.menus;//数据回填
            },
            //保存路径
            saveNavState(activePath){
                window.sessionStorage.setItem('activePath',activePath);//存放在session里
                this.activePath=activePath;
            }
        }
    };
</script>

<style scoped >
    .home-container{
        height:900px;
    }
    /*头样式*/
    .el-header {
        background-color: #B3C0D1;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
         color: #fff;
        font-size: 20px;
        line-height: 60px;
    }
    .header-div{
        display: flex;
        align-items: center;
    }
    . header-span{
        margin-left: 15px;
    }
    /*侧边样式*/
    .el-aside {
        background-color: #545c64;
    }
    .el-menu{
        border-right: none;
    }
    /*主体样式*/
    .el-main{
        background-color: #eaedf1;
    }
    img{
       width: 55px;
        height: 55px;
    }

</style>